package org.example.demo;

import java.util.HashMap;
import java.util.Map;

/**
 * 超市类
 */
public class Supermarket {
    // 超市库存，键为水果名称，值为水果对象
    private Map<String, Fruit> inventory;

    public Supermarket() {
        inventory = new HashMap<>();
        // 初始化超市库存
        inventory.put("apple", new Apple());
        inventory.put("strawberry", new Strawberry());
        inventory.put("mango", new Mango());
    }

    // 计算购买指定水果的总价
    public double calculateTotalPrice(String fruitName, double weightInKg) {
        Fruit fruit = inventory.get(fruitName);
        if (fruit != null) {
            return fruit.calculatePrice(weightInKg);
        } else {
            System.out.println("超市暂时没有" + fruitName);
            // 如果水果不存在，返回价格为0
            return 0.0;
        }
    }
}
